//
//  LanguageViewController.swift
//  AreaChainblock
//
//  Created by 赵佟越 on 2018/9/19.
//  Copyright © 2018年 zhaotongyue. All rights reserved.
//

import UIKit
import SVProgressHUD

import Eureka

class LanguageViewController: BaseViewController , UITableViewDelegate , UITableViewDataSource{
    var LanguageArr = [XALanguageManager.getString(key: "中文"),XALanguageManager.getString(key: "英文"),XALanguageManager.getString(key: "繁體中文"),"Français","日本語","Deutsch","русский язык","Español"] as [Any]
    override func viewDidLoad() {
        super.viewDidLoad()
        makeBaseUI()
        self.title = XALanguageManager.getString(key: "语言")
        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    //MARK: - Private Methods
    func makeBaseUI(){
        // ui
        self.view.addSubview(myTableView)
        
    }
    //MARK: - tableView Delegate And DataSource
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       var cell:SettingTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cell")as?SettingTableViewCell
        if cell == nil {
            cell = SettingTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
        }
        cell?.textLabel?.text = LanguageArr[indexPath.row] as? String
        cell.textLabel?.textColor = UIColor.white
        return cell!
    }
    //footer height
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    // row height
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    // header height
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return nil
    }
    
    // row num
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return LanguageArr.count
    }
    // 组数
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    //点击事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.row == 0 {
            XALanguageManager.shareInstance.setLanguage(langeuage: "zh-Hans")
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LanguageChanged"), object: nil)
            self.present(ExampleProvider.customIrregularityStyle(delegate: nil), animated: true) {
                
            }
            
        }else if indexPath.row == 1{
            
            XALanguageManager.shareInstance.setLanguage(langeuage: "en")
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LanguageChanged"), object: nil)
            self.present(ExampleProvider.customIrregularityStyle(delegate: nil), animated: true) {
                
            }

        }else if indexPath.row == 2{
            XALanguageManager.shareInstance.setLanguage(langeuage: "zh-Hant")
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LanguageChanged"), object: nil)
            self.present(ExampleProvider.customIrregularityStyle(delegate: nil), animated: true) {
                
            }
        }else{
            SVProgressHUD.showError(withStatus: XALanguageManager.getString(key: "ip不符"))
            SVProgressHUD.dismiss(withDelay: 0.6)
        }
    }
    
    override func willMove(toParentViewController parent: UIViewController?) {
        
    }
    
    // MARK: - 懒加载
    private lazy var myTableView:UITableView = {
        var tempheight = CGFloat()
        tempheight = 64
        if #available(iOS 11.0.0, *) {
            tempheight = 0
            print("iOS 9.3.5 is available")
        }
        let myTableView = UITableView(frame: CGRect(x: 0, y: tempheight, width: kScreenWidth, height: kScreenHeight - tempheight), style: UITableViewStyle.grouped)
        myTableView.delegate = self
        myTableView.backgroundColor = UIColor.clear
        myTableView.dataSource = self
        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        return myTableView
    }()
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
